﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using mshtml;
using TeacherAppraiser.Properties;
using System.Threading;

namespace TeacherAppraiser
{
    public partial class Browser : Form
    {
        public Browser()
        {
            InitializeComponent();
        }
        
        public string UrlValue { get; set; }
        public string StuId { get; set; }
        public string StuPw { get; set; }

        private static int _pageType;
        private static int _subjectCount;

        private void Browser_Load(object sender, EventArgs e)
        {
            wbAppraiser.Url = new Uri(@"http://"+UrlValue+@"/");
            lbDescription.Text = Resources.txtDescription;
        }

        private void btAppraising_Click(object sender, EventArgs e)
        {
            try
            {
                if (wbAppraiser.Document == null) return;
                if (wbAppraiser.Document.Window == null) return;
                if (wbAppraiser.Document.Window.Frames == null) return;
                var farmeA = wbAppraiser.Document.Window.Frames["bottomFrame"];
                if (farmeA == null) return;
                if (farmeA.Document == null) return;
                if (farmeA.Document.Window == null) return;
                if (farmeA.Document.Window.Frames == null) return;
                var farmeB = farmeA.Document.Window.Frames["mainFrame"];
                if (farmeB == null) return;
                if (farmeB.Document == null) return;
                var formA = farmeB.Document.All["StDaForm"];
                if (formA == null) return;
                var elementA = formA.GetElementsByTagName("input");
                var numId = 4;
                var btnRadio = elementA.GetElementsByName(string.Format("{0:D10}", numId));
                btnRadio[0].InvokeMember("click");
                while (numId < 25)
                {
                    numId++;
                    btnRadio = elementA.GetElementsByName(string.Format("{0:D10}", numId));
                    if(numId==24)
                    {
                        btnRadio[3].InvokeMember("click");
                    }
                    else
                    {
                        btnRadio[0].InvokeMember("click");
                    }
                }
                var textArea = formA.GetElementsByTagName("textarea");
                textArea[0].InnerText = "老师的言传身教给我留下了很深的印象";

                if (formA.Document == null) return;
                var jsCode = formA.Document.CreateElement("script");
                if (jsCode == null) return;
                jsCode.SetAttribute("type", "text/javascript");
                jsCode.SetAttribute("text", "window.document.StDaForm.submit();");
                if (formA.Document.Body == null) return;
                formA.Document.Body.AppendChild(jsCode);
            }
            catch (Exception)
            {
                MessageBox.Show("亲，请确定这是评教页面，还有难不成你已经给这个老师评过了？");
            }
        }

        private void wbAppraiser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            switch (_pageType)
            {
                case 0:
                    if (wbAppraiser.Document == null) return;
                    var tbUserid = wbAppraiser.Document.All["zjh"];
                    var tbPasswd = wbAppraiser.Document.All["mm"];

                    if (tbUserid == null || tbPasswd == null) return;

                    tbUserid.SetAttribute("value", StuId);
                    tbPasswd.SetAttribute("value", StuPw);

                    if (wbAppraiser.Document == null) return;
                    var btLogin = wbAppraiser.Document.All["btnSure"];
                    if (btLogin == null) return;
                    wbAppraiser.Document.InvokeScript("login");
                    break;
                case 1:
                    if (wbAppraiser.Document == null) return;
                    if (wbAppraiser.Document.Window == null) return;
                    if (wbAppraiser.Document.Window.Frames == null) return;
                    var farmeA = wbAppraiser.Document.Window.Frames["bottomFrame"];
                    if (farmeA == null) return;
                    if (farmeA.Document == null) return;
                    if (farmeA.Document.Window == null) return;
                    if (farmeA.Document.Window.Frames == null) return;
                    var farmeB = farmeA.Document.Window.Frames["mainFrame"];
                    if (farmeB == null) return;
                    if (farmeB.Document == null) return;
                    var formA = farmeB.Document.All["WjList"];
                    if (formA == null ) return;
                    var elementA = formA.GetElementsByTagName("img");
                    if (_subjectCount >= elementA.Count - 5)
                    {
                        _subjectCount = 0;
                        _pageType = 3;
                        MessageBox.Show(
                            "自动评教已经完成!\n为了防止意外，请检查全部科目的评价状态为“是”;",
                            "自动评教完成",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information
                            );
                        return;
                    }
                    elementA[_subjectCount].InvokeMember("click");
                    _subjectCount++;
                    _pageType = 2;
                    break;
                case 2:
                    try
                    {
                        if (wbAppraiser.Document == null) return;
                        if (wbAppraiser.Document.Window == null) return;
                        if (wbAppraiser.Document.Window.Frames == null) return;
                        farmeA = wbAppraiser.Document.Window.Frames["bottomFrame"];
                        if (farmeA == null) return;
                        if (farmeA.Document == null) return;
                        if (farmeA.Document.Window == null) return;
                        if (farmeA.Document.Window.Frames == null) return;
                        farmeB = farmeA.Document.Window.Frames["mainFrame"];
                        if (farmeB == null) return;
                        if (farmeB.Document == null) return;
                        formA = farmeB.Document.All["StDaForm"];
                        if (formA == null) return;
                        elementA = formA.GetElementsByTagName("input");
                        var numId = 4;
                        var btnRadio = elementA.GetElementsByName(string.Format("{0:D10}", numId));
                        btnRadio[0].InvokeMember("click");
                        while (numId < 25)
                        {
                            numId++;
                            btnRadio = elementA.GetElementsByName(string.Format("{0:D10}", numId));
                            if (numId == 24)
                            {
                                btnRadio[3].InvokeMember("click");
                            }
                            else
                            {
                                btnRadio[0].InvokeMember("click");
                            }
                        }
                        var textArea = formA.GetElementsByTagName("textarea");
                        textArea[0].InnerText = "老师的言传身教给我留下了很深的印象";

                        if (formA.Document == null) return;
                        var jsCode = formA.Document.CreateElement("script");
                        if (jsCode == null) return;
                        jsCode.SetAttribute("type", "text/javascript");
                        jsCode.SetAttribute("text", "window.document.StDaForm.submit();");
                        if (formA.Document.Body == null) return;
                        formA.Document.Body.AppendChild(jsCode);
                        
                        Thread.SpinWait(100);

                        if (wbAppraiser.Document == null) return;
                        if (wbAppraiser.Document.Window == null) return;
                        if (wbAppraiser.Document.Window.Frames == null) return;
                        farmeA = wbAppraiser.Document.Window.Frames["topFrame"];
                        if (farmeA == null) return;
                        if (farmeA.Document == null) return;
                        formA = farmeA.Document.All["mainForm"];
                        if (formA == null) return;
                        elementA = formA.GetElementsByTagName("a");
                        if (elementA.Count < 5) return;
                        elementA[4].InvokeMember("click");
                        _pageType = 1;
                    }
                    catch (Exception)
                    {
                        if (wbAppraiser.Document == null) return;
                        if (wbAppraiser.Document.Window == null) return;
                        if (wbAppraiser.Document.Window.Frames == null) return;
                        farmeA = wbAppraiser.Document.Window.Frames["topFrame"];
                        if (farmeA == null) return;
                        if (farmeA.Document == null) return;
                        formA = farmeA.Document.All["mainForm"];
                        if (formA == null) return;
                        elementA = formA.GetElementsByTagName("a");
                        if (elementA.Count < 5) return;
                        elementA[4].InvokeMember("click");
                        _pageType = 1;
                    }
                    break;
                default:
                    break;
            }


        }

        private void btnAuto_Click(object sender, EventArgs e)
        {
            try
            {
                if (wbAppraiser.Document == null) return;
                if (wbAppraiser.Document.Window == null) return;
                if (wbAppraiser.Document.Window.Frames == null) return;
                var farmeA = wbAppraiser.Document.Window.Frames["topFrame"];
                if (farmeA == null) return;
                if (farmeA.Document == null) return;
                var formA = farmeA.Document.All["mainForm"];
                if (formA == null) return;
                var elementA = formA.GetElementsByTagName("a");
                if (elementA.Count < 5) return;
                elementA[4].InvokeMember("click");
                _pageType = 1;
            }
            catch (Exception)
            {
                MessageBox.Show("1");
            }
        }

        private void wbAppraiser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            try
            {
                if (wbAppraiser.Document == null) return;
                if (wbAppraiser.Document.Window == null) return;
                if (wbAppraiser.Document.Window.Frames == null) return;
                var farmeA = wbAppraiser.Document.Window.Frames["bottomFrame"];
                if (farmeA == null) return;
                if (farmeA.Document == null) return;
                if (farmeA.Document.Window == null) return;
                if (farmeA.Document.Window.Frames == null) return;
                var farmeB = farmeA.Document.Window.Frames["mainFrame"];
                if (farmeB == null) return;
                if (farmeB.Document == null) return;
                if (farmeB.Document.Window == null) return;
                var win = (IHTMLWindow2)farmeB.Document.Window.DomWindow;
                const string s = @"window.alert = null; window.onerror = null; window.confirm = null; window.open = null; window.showModalDialog = null;";
                win.execScript(s, "javascript");
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
